
//
// 120322 - AH - (Midtun) Created.
// 120325a- AH - removeParameter(String) and setParameters(Map<String, Object>).
// 120325b- AH - getParameterMap(String...).
// 120326 - AH - (Huset) setAttributes(AttributeObject).
// 120329a- AH - (Midtun) Support for storing chat input: 
//               addChatInputLine(String) and getChatInputLines().
// 120329 - AH - (Midtun) begin().
// 120401 - AH - Now extends ParameterBase.
//

package org.soekrat.server.io.db;

import java.util.ArrayList;
import java.util.List;

import org.soekrat.AttributeObject;
import org.soekrat.engine.SoekratEngine;
import org.soekrat.engine.visitor.Lines;

import com.googlecode.objectify.annotation.Subclass;

/**
 * <p>
 *   Represents a conversation element in the database.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@Subclass
public final class ConversationElement extends ParameterBase 
{
  private long ownerId;
  
  private List<String> chatInput=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ConversationElement(){}
  
  /**
   * <p>
   *   Sets owner
   *   ({@link Account} key) of {@code this}.
   * </p>
   * @param owner Key to owner.
   * @return {@code this}.
   */
  public ConversationElement ownerId(Long owner)
  {
    if (owner<0)
    {
      throw new IllegalArgumentException("owner<0 : "+owner);
    }
    
    this.ownerId=owner;
    
    return this;
  }
  
  /**
   * <p>
   *   Gets owner
   *   ({@link Account} key) of {@code this}.   
   * </p>
   * @return Key.
   */
  public long ownerId(){ return this.ownerId; }
  
  @Override
  public String toString() 
  {
    StringBuilder sb=new StringBuilder();    
    return sb.append("id=").append(id()).append(", owner=").append(ownerId)
             .append(", ").append(super.toString()).toString();
  }
  
  /**
   * <p>
   *   Sets this' data as attributes to a given
   *   {@link AttributeObject}.
   * </p>
   * <p>
   *   Property 
   *   {@link #id} is set as attribute named "id",
   *   {@link #ownerId} is set as attribute named "owner" and all other proper
   *   {@link #getParameter(String) attributes} is set with their names they
   *   have here.
   * </p>
   * @param ao {@code AttributeObject} to receive.
   */
  public void setAttributes(AttributeObject ao)
  {
    if (ao==null)
    {
      throw new NullPointerException("ao");
    }
    
    ao.setAttribute("id", id());
    ao.setAttribute("owner", ownerId());
    
    super.setAttributes(ao);
  }
  
  /**
   * <p>
   *   Invoked when {@code this} is set in state to hit the
   *   {@link SoekratEngine} one more time.
   * </p>
   */
  public void begin()
  {
    this.chatInput=null;
  }
  
  /**
   * <p>
   *   Adds a chat input line.
   * </p>
   * @param l Line to add, if {@code null} or the empty string is ignored.
   */
  public void addChatInputLine(String l)
  {
    l=(l==null) ? "" : l.trim();
    
    if (l.length()==0) return;
    
    if (this.chatInput==null) this.chatInput=new ArrayList<String>();
    this.chatInput.add(l);
  }
  
  /**
   * <p>
   *   Gets visitor last chat input as
   *   {@link Lines}.
   * </p>
   * @return {@code Lines}.
   */
  public Lines getChatInputLines()
  {
    Lines retVal=new Lines();
    
    if (this.chatInput!=null)
    {
      for (String l : this.chatInput) retVal.add(l);
    }
    
    return retVal;
  }
  
}
